package com.ruoyi.project.digital.service.task;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.digital.domain.*;
import com.ruoyi.project.digital.service.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class ChargTask {

    @Autowired
    private IPetMessageService messageService;
    @Autowired
    private IPetMessageDetailsService detailsService;
    @Autowired
    private ITaskService taskService;
    @Autowired
    private IUserTaskService userTaskService;
    @Autowired
    private ITUserService userService;

    /**
     * 定时任务结束回话请求
     */
    @Scheduled(fixedRate = 10000000)
    public void calOptableCask() {
        QueryWrapper<PetMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("off_time");
        List<PetMessage> petMessageList = messageService.list(queryWrapper);
        for (PetMessage petMessage : petMessageList) {
            if (ObjectUtil.isNotNull(petMessage.getStartTime())) {
                QueryWrapper<PetMessageDetails> detailsQueryWrapper = new QueryWrapper<>();
                detailsQueryWrapper.eq("message_id", petMessage.getId());
                detailsQueryWrapper.orderByDesc("id");
                List<PetMessageDetails> detailsList = detailsService.list(detailsQueryWrapper);
                Date timeNow = DateUtils.getNowDate();
                if (ObjectUtil.isNotNull(detailsList) && detailsList.size() != 0) {
                    PetMessageDetails details = detailsList.get(0);
                    Date times = details.getCreateTime();
                    //如果超过10分钟,则回话结束
                    if (ObjectUtil.isNull(petMessage.getStartTime())) {
                        petMessage.setOffTime(DateUtils.getNowDate());
                        messageService.updateById(petMessage);
                    }
                    if (timeNow.getTime() - petMessage.getStartTime().getTime() > 600000) {
                        petMessage.setOffTime(DateUtils.getNowDate());
                        messageService.updateById(petMessage);
                    }
                } else {
                    //如果超过10分钟,则回话结束
                    if (timeNow.getTime() - petMessage.getStartTime().getTime() > 600000) {
                        messageService.updateById(petMessage);
                    }
                }

            }
        }

    }

    /**
     * 定时任务-创建每日任务
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void setUserTaskService() {
        QueryWrapper<Task> taskQueryWrapper = new QueryWrapper<>();
        taskQueryWrapper.eq("is_day", "0");
        List<Task> taskList = taskService.list(taskQueryWrapper);
        List<TUser> tUserList = userService.list();
        for (Task task : taskList) {
            for (TUser user : tUserList) {
                QueryWrapper<UserTask> userTaskQueryWrapper = new QueryWrapper<>();
                userTaskQueryWrapper.eq("user_id", user.getId());
                userTaskQueryWrapper.eq("task_id", task.getId());
                userTaskQueryWrapper.eq("create_time", new Date());
                List<UserTask> userTaskList = userTaskService.list(userTaskQueryWrapper);
                if (userTaskList.size() == 0) {
                    UserTask userTask = new UserTask();
                    userTask.setUserId(user.getId());
                    userTask.setTaskId(task.getId());
                    userTask.setGetTime(DateUtils.getNowDate());
                    userTask.setTaskType(task.getIsDay());
                    userTask.setTitle(task.getTitle());
                    userTask.setTaskName(task.getNeedKey());
                    if (null != task.getGetValue()){
                        userTask.setGameValue(String.valueOf(task.getGetValue()));
                    }
                    userTask.setCreateTime(new Date());
                    userTask.setStatus("0");
                    userTaskService.save(userTask);
                }
            }
        }
    }

    /**
     * 定时任务-过期每日任务
     */
    @Scheduled(cron = "0 0 23 * * ?")
//    @Scheduled(cron = "0 */1 * * * ?")
    public void setUserTaskObsoleteService() {
        // 过期 待完成任务
        List<String> strings = new ArrayList<>();
        strings.add("0");
        List<TUser> tUserList = userService.list();
        for (TUser user : tUserList) {
            UpdateWrapper<UserTask> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_id", user.getId());
            updateWrapper.eq("task_type", "0");
            updateWrapper.in("status", strings);
//            updateWrapper.eq("create_time", new Date());
            updateWrapper.set("status","2");
            userTaskService.update(updateWrapper);
        }
    }

}
